<?php
/**
 * oscshop 电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    李梓钿
 *
 */
namespace Admin\Controller;
use Admin\Model\AuthGroupModel;

class AuthGroupController extends CommonController{
	public function index_old(){
		$model=new AuthGroupModel();

		$data=$model->show_Auth_group_page();

		$this->assign('empty',$data['empty']);// 赋值数据集

		$this->assign('list',$data['list']);// 赋值数据集
		$this->assign('page',$data['page']);// 赋值分页输出

		$this->display();
	}

	public function index(){

		$count=M('admin')->count();
		$list=M('admin')->select();
		$Page       = new \Think\Page($count,25);
		$show       = $Page->show();
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display();
	}

	public function setGroupAuth(){
		$model=new AuthGroupModel();

	}

/*	public function add(){

		$model=new AuthGroupModel();
		if(IS_POST){

			$data=I('post.');

			$return=$model->add_auth_group($data);

			$this->ajaxReturn($return);
		}
		$cate = M()->query('SELECT id,pid,title AS name,is_update,is_delete  FROM '.C('DB_PREFIX').'menu ORDER BY sort_order ASC');

		$list =list_to_tree($cate);
		$list = json_encode($list);
		$this->assign('menuList',$list);// 赋值数据集
		$this->crumbs='新增';
		$this->action=U('AuthGroup/add');

		$this->display();
	}*/


	public function add(){
		$uid=I('get.id');
		$where['a_id']=$uid;
		$jurisdiction=M('admin')->where($where)->field('jurisdiction')->find();

		$this->assign('jurisdiction',$jurisdiction['jurisdiction']);// 赋值数据集
		$this->crumbs='新增';
		$this->action=U('AuthGroup/add');

		$this->display();
	}

	public function edit(){
		$model=new AuthGroupModel();

		$this->crumbs='编辑';
		$this->action=U('AuthGroup/edit');
		$authGroup=M('AuthGroup')->find(I('id'));
		if(IS_POST){
			$data=I('post.');

			$return=$model->edit_auth_group($data,$authGroup['title']);
			$this->ajaxReturn($return);

		}
		$this->data = $authGroup;
		$rules = $authGroup['rules'];
		$rules = explode(",",$rules);

		$cate = M()->query('SELECT id,pid,title AS name FROM '.C('DB_PREFIX').'menu ORDER BY sort_order ASC');
		foreach($cate as $key =>$value){
			foreach($rules as $v){
				$auth = $value['id'];
				if($auth==$v){
					if(isset($cate[$key]['children'])&&!empty($cate[$key]['children'])){
						$children = $cate[$key]['children'];
						foreach($children as $a => $cv){
							if($v ==$cv['id'] ){
								$children['checked'] = true;
								break;
							}else{
								$children['checked'] = false;
							}
						}
					}
					$cate[$key]['checked']=true;
					break;
				}else{
					$cate[$key]['checked']=false;
				}

			}
		}
		$list =list_to_tree($cate);
		$list = json_encode($list);
		$this->assign('menuList',$list);// 赋值数据集
		$this->display();
	}

	public function delete(){

	}

	 public function treeList(){

		 $cate = M()->query('SELECT id,pid,title  AS name,is_update,is_delete FROM '.C('DB_PREFIX').'menu ORDER BY sort_order ASC');

		 $list =list_to_tree($cate);

		// $list = json_encode($list);
		 $data = array(err_code => 1001, err_msg => $list);
		 $this->ajaxReturn($data, 'JSON');
	 }


	public function addadmin(){
         if(IS_POST){
              $name=I('post.u_name');
              $pass=I('post.password');
              $strIds=I('post.ids');
              $true_name=I('post.name');

			 if(!$name || !$pass || !$strIds){
				 $data = array(err_code => 1201, err_msg => '用户名，密码，权限不能为空');
				 $this->ajaxReturn($data, 'JSON');
			 }
			 $str=implode(',',$strIds);

			 $abwhere['a_uname']=$name;
			 $is_exist=M('admin')->where($abwhere)->find();
			 if($is_exist){
				 echo 2;exit;
			 }
			 $passw=think_ucenter_encrypt($pass,C('PWD_KEY'));
			 $u=session('user_auth');
				 $data['a_uname']=$name;
			 $data['a_passwd']=$passw;
			 $data['create_user']=$u['username'];
			 $data['a_create_time']=time();
			 $data['jurisdiction']=$str;
			 $data['a_true_name']=$true_name;
			 $data['a_status']=1;

			 $a=M('admin')->add($data);
			 echo 1;exit;

		 }
		$this->display();
	}
}
?>